package net.gyapp.markim.jpush;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.easyder.wrapper.utils.LogUtils;

import net.gyapp.markim.R;
import net.gyapp.markim.core.ImHelper;
import net.gyapp.markim.core.entity.MessageEntity;
import net.gyapp.markim.module.basic.MainActivity;
import net.gyapp.markim.utils.NotifyUtil;

import java.util.List;

import cn.jpush.android.api.JPushInterface;


public class CustomReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
//        LogUtils.info("CustomReceiver 收到通知");
        try {
            Bundle bundle = intent.getExtras();
            String title = bundle.getString(JPushInterface.EXTRA_TITLE);
            String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
            String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
            LogUtils.info("CustomReceiver [MyReceiver] onReceive - " + intent.getAction() + ", title" + title + " message " + message + " extras " + extras);

            if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
                String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);

                LogUtils.info("CustomReceiver [MyReceiver] 接收Registration Id : " + regId);
                //send the Registration Id to your server...

            } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {     //收到自定义消息 需自行处理
//                String title = bundle.getString(JPushInterface.EXTRA_TITLE);
//                String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
//                String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
                LogUtils.info("CustomReceiver 接收到推送下来的自定义消息 --> title" + title + " message " + message + " extras " + extras);
                LogUtils.info("--> Receiver 外部通知");
                onPauseMessageReceive(context);
            } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {  //收到通知先走这一步
                LogUtils.info("CustomReceiver [MyReceiver] 接收到推送下来的通知");
                int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID);
//                LogUtils.info("CustomReceiver [MyReceiver] 接收到推送下来的通知的ID: " + notifactionId);

            } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) { //收到通知被点击
                LogUtils.info("CustomReceiver [MyReceiver] 用户点击打开了通知" + bundle.getString(JPushInterface.EXTRA_ALERT) + "  -- " + bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE));


            } else if (JPushInterface.ACTION_RICHPUSH_CALLBACK.equals(intent.getAction())) {
                LogUtils.info("CustomReceiver [MyReceiver] 用户收到到RICH PUSH CALLBACK: " + bundle.getString(JPushInterface.EXTRA_EXTRA));
                //在这里根据 JPushInterface.EXTRA_EXTRA 的内容处理代码，比如打开新的Activity， 打开一个网页等..

            } else if (JPushInterface.ACTION_CONNECTION_CHANGE.equals(intent.getAction())) {
                boolean connected = intent.getBooleanExtra(JPushInterface.EXTRA_CONNECTION_CHANGE, false);
                LogUtils.info("CustomReceiver [MyReceiver]" + intent.getAction() + " connected state change to " + connected);

                String regId = bundle.getString(JPushInterface.EXTRA_REGISTRATION_ID);
//                LogUtils.info("CustomReceiver [MyReceiver] 接收Registration Id : " + regId);
            } else {
                LogUtils.info("CustomReceiver [MyReceiver] Unhandled intent - " + intent.getAction());
            }
        } catch (Exception e) {
        }
    }

    /**
     * 当应用非在主界面时回调
     */
    public static void onPauseMessageReceive(Context context) {
        List<MessageEntity> list = ImHelper.getLatestMessage();
        if (list != null && list.size() > 0) {
            MessageEntity entity = list.get(0);
            Intent intent = new Intent(context, MainActivity.class);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.setAction(Intent.ACTION_MAIN);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            PendingIntent pIntent = PendingIntent.getActivity(context,
                    1001, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            new NotifyUtil(context, 1001).notify_normal_singline(pIntent, R.mipmap.icon, entity.content, context.getString(R.string.app_name),
                    entity.content, true, true, false);
        }
    }

}